package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.FileConfigForm;
import com.dimples.dd.system.model.query.FileConfigPageQuery;
import com.dimples.dd.system.model.vo.FileConfigRespVO;
import com.dimples.dd.system.service.SystemFileConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.dimples.dd.common.result.CommonResult.success;


@Tag(name = "09.文件配置")
@RestController
@RequestMapping("/file-config")
@Validated
@RequiredArgsConstructor
public class FileConfigController {

    private final SystemFileConfigService fileConfigService;

    @PostMapping
    @Operation(summary = "创建文件配置")
    @PreAuthorize("@ss.hasPerm('system:file-config:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Long> createFileConfig(@Valid @RequestBody FileConfigForm fileConfigForm) {
        return success(fileConfigService.createFileConfig(fileConfigForm));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新文件配置")
    @PreAuthorize("@ss.hasPerm('system:file-config:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateFileConfig(
            @PathVariable Long id,
            @Valid @RequestBody FileConfigForm fileConfigForm) {
        fileConfigForm.setId(id);
        fileConfigService.updateFileConfig(fileConfigForm);
        return success(true);
    }

    @PatchMapping("/update-master/{id}")
    @Operation(summary = "更新文件配置为 Master")
    @PreAuthorize("@ss.hasPerm('system:file-config:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateFileConfigMaster(@PathVariable("id") Long id) {
        fileConfigService.updateFileConfigMaster(id);
        return success(true);
    }

    @DeleteMapping
    @Operation(summary = "删除文件配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPerm('system:file-config:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteFileConfig(@RequestParam("id") Long id) {
        fileConfigService.deleteFileConfig(id);
        return success(true);
    }

    @GetMapping("/{id}/form")
    @Operation(summary = "获得文件配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPerm('system:file-config:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<FileConfigRespVO> getFileConfig(@PathVariable("id") Long id) {
        FileConfigRespVO config = fileConfigService.getFileConfig(id);
        return success(config);
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件配置分页")
    @PreAuthorize("@ss.hasPerm('system:file-config:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<FileConfigRespVO> getFileConfigPage(@Valid FileConfigPageQuery pageQuery) {
        return fileConfigService.getFileConfigPage(pageQuery);
    }

    @GetMapping("/test/{id}")
    @Operation(summary = "测试文件配置是否正确")
    @PreAuthorize("@ss.hasPerm('system:file-config:query')")
    @ApiLog(operateType = OperateTypeEnum.OTHER, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<String> testFileConfig(@PathVariable("id") Long id) throws Exception {
        String url = fileConfigService.testFileConfig(id);
        return success(url);
    }
}
